package com.liu.es.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.io.IOException;

public class ES文档操作 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient rest = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));


//        IndexRequest index = new IndexRequest();
//
//        GetRequest index = new GetRequest();

//        UpdateRequest index = new UpdateRequest();
        DeleteRequest index = new DeleteRequest();
        index.index("user").id("1001");
        //更新
//        index.doc(XContentType.JSON,"sex","male");
        //文档增加
//        User user = new User("lcm", "female", 25);
//        ObjectMapper om = new ObjectMapper();
//        String json = om.writeValueAsString(user);
        //用source加入数据，并规定数据格式
//        index.source(json, XContentType.JSON);
        //查看、删除、修改、添加对应的方法是get、delete、update、index，每个方法返回的对象不相同
//        UpdateResponse i = rest.update(index, RequestOptions.DEFAULT);
//        rest.update();//更新文档
//        rest.index();//创建文档
//        rest.delete();//删除文档
//        rest.get()//查看文档
        DeleteResponse i = rest.delete(index, RequestOptions.DEFAULT);


        System.out.println(i.toString());


        rest.close();
    }

    @Test
    public void opera_Doc() throws IOException {
        RestHighLevelClient test = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        //若要写入对象，则要转换成json，用到ObjectMapper
        ObjectMapper om = new ObjectMapper();
        User u = new User();
        u.setName("DDD");
        u.setAge(26);
        u.setSex("男");
        String result = om.writeValueAsString(u);
        //创建文档添加对象
//        IndexRequest doc = new IndexRequest();

        //创建文档获取对象
        GetRequest doc = new GetRequest();

        //获取文档删除对象
//        DeleteRequest doc = new DeleteRequest();


        //绑定索引和命名id
        doc.index("user").id("0");

        //为索引添加文档
//        doc.source(result,XContentType.JSON);

        //添加文档
//        IndexResponse res = test.index(doc, RequestOptions.DEFAULT);

        //获取文档
        GetResponse res = test.get(doc, RequestOptions.DEFAULT);

        //删除文档
//        DeleteResponse res = test.delete(doc, RequestOptions.DEFAULT);

        System.out.println(res);

        test.close();
    }
}
